package com.xiexiaozhou.chapter423.Controller;

import com.xiexiaozhou.chapter423.utils.ResultGenerator;
import com.xiexiaozhou.chapter423.vo.Result;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/hello")
public class HelloController {

    //原本返回结果
    /*{
        "timestamp": 1526403302390,
            "status": 500,
            "error": "Internal Server Error",
            "exception": "java.lang.ArithmeticException",
            "message": "/ by zero",
            "path": "/api/hello/say/"
    }*/

    //处理后返回结果
    /*{
            "message": "未知错误，请联系系统管理员",
            "data": "错误信息：/ by zero",
            "status": 500
        }
    }*/

    //第一种方法
    //http://localhost:8080/api/hello/say/?name=AOP
 /*   @GetMapping("/say")
    public Result<String> say(@RequestParam String name) {
        try {
            //模拟异常信息
            int a = 2;
            int b = 0;
            int c = a / b;

            System.out.println(c);
            return ResultGenerator.ok("Hello," + name + c);
        } catch (Exception e) {
            return ResultGenerator.fail500(e.getMessage());
        }
*/

    //第二种方法
    //http://localhost:8080/api/hello/say/?name=AOP
 /*   @GetMapping("/say")
    public Result<String> say(@RequestParam String name) {

        //模拟异常信息
        int a = 2;
        int b = 0;
        int c = a / b;

        System.out.println(c);
        return ResultGenerator.ok("Hello," + name + c);
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler() {
        return ResultGenerator.fail();
    }
*/

    //第三种方法
    //继承GolablExceptionEndpoint父类
    //http://localhost:8080/api/hello/say/?name=AOP
    @GetMapping("/say")
    public Result<String> say(@RequestParam String name) {

        //模拟异常信息
        int a = 2;
        int b = 0;
        int c = a / b;

        System.out.println(c);
        return ResultGenerator.ok("Hello," + name + c);
    }
}
